import { GetListByPage } from '@/axios/api/CashFlowAPI'

const cashflow = {
    namespaced: true,
    state: () => ({  
        tabledata: [],
        showmodal: false,
        title: '',
        creatrouter: '',
        editrouter: '',
        fullscreen: true,
        loadingtable: false,
        tablecolumns: [],
        currentCashFlowID: '',
        total: 0,
        pageIndex: 1,
        pageSize: 15,
        pageSizeOpts: [15,25,50,100]
    }),
    mutations: { 
        UPDATE_TABLE_COLUMNS: (state,payload) => {
            //console.log(payload)
            payload = [
                {
                    type: 'selection',
                    minWidth: 60,
                    align: 'center',
                    fixed: 'left'
                },
                {
                    type: 'index',
                    key: 'Index',
                    title: '序号',
                    minWidth: 70,
                    align: 'center',
                    fixed: 'left'
                },
                {
                    title: '项目地点',
                    key: 'ProjectAddress',
                    align:'center',
                    minWidth: 220,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.ProjectAddress)
                    }
                },
                {
                    title: '账户尾号',
                    key: 'BankAccountTail',
                    align:'center',
                    minWidth: 70,
                    fixed: 'left',
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.BankAccountTail)
                    }
                },
                {
                    title: '银行名称',
                    key: 'BankName',
                    align:'center',
                    minWidth: 300,
                    fixed: 'left',
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.BankName)
                    }
                },
                {
                    title: '银行账户',
                    key: 'BankAccount',
                    align:'center',
                    minWidth: 200,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.BankAccount)
                    }
                },
                {
                    title: '项目编码',
                    key: 'ProjectNumCode',
                    align:'center',
                    minWidth: 130,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.ProjectNumCode)
                    }
                },
                {
                    title: '项目名称',
                    key: 'ProjectName',
                    align:'center',
                    minWidth: 260,
                    fixed: 'left',
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.ProjectName)
                    }
                },
                {
                    title: '款项状态',
                    key: 'FundsStatus',
                    align:'center',
                    minWidth: 100,
                    render:(h,params)=>{
                        return params.row.FundsStatus == "1" ? h('span','已确认') : h('span','待确认')
                    }
                },
                {
                    title: '金额方向',
                    key: 'MoneyFoward',
                    align: 'center',
                    minWidth: 70,
                    render:(h,params) => {
                        var moneytype = params.row.MoneyForward;
                        if(Number.parseInt(moneytype)==0){
                            return h('span','收入');
                        }
                        if(Number.parseInt(moneytype)==1){
                            return h('span','支出');
                        }
                    }
                },
                {
                    title: '金额',
                    key: 'Amount',
                    align:'center',
                    minWidth: 150,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.Amount)
                    }
                },
                {
                    title: '款项类型',
                    key: 'FundsType',
                    align:'center',
                    minWidth: 120,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.FundsType)
                    }
                },
                {
                    title: '落实人',
                    key: 'PayOperator',
                    align:'center',
                    minWidth: 100,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.PayOperator)
                    }
                },
                {
                    title: '调拨日期',
                    key: 'AllocationTime',
                    sortable: true,
                    align:'center',
                    fixed:'left',
                    minWidth: 120,
                    render:(h,params)=>{
                        return h('div',{style: { textAlign:"left" }},params.row.AllocationTime)
                    }
                }
            ]
            state.tablecolumns = payload;
        },
        UPDATE_TABLE_DATA: (state,payload) => {
            //console.log(payload)
            /* payload = [
                {
                    Guid: '17gd-djgk-gdkgj-gdkf12-dfg',
                    CashFlowName: '交行诸暨支行',
                    CashFlowAccount: '1211024409059906581',
                    AccountTail: '6581',
                    AccountType: '一般户',
                    BeginBalance: 0.00,
                    EndingBalance: 0.00,
                    FreezeMoney: 0.00,
                    FlowBalance: 0.00
                }
            ]; */
            state.total = payload.totalCount;
            state.tabledata = payload.items;
        },
        CHANGE_TITLE: (state,payload) => {
            state.title = payload;
        },
        CHANGE_MODAL: (state) => {
            state.showmodal = !state.showmodal;
        },
        SETTING_ID: (state,payload) => {
            state.currentCashFlowID= payload;
        },
        SETTING_PAGEINDEX: (state,payload) =>{
            state.pageIndex = payload;
        },
        SETTING_PAGESIZE: (state,payload) => {
            state.pageSize = payload;
        },
        SETTING_LOADTABLE: (state,payload) => {
            state.loadingtable = payload
        },
        SETTING_CREATROUTER: (state,payload) => {
            state.creatrouter = payload;
        },
        SETTING_EDITROUTER: (state,payload) => {
            state.editrouter = payload;
        },
        CREATE: (state,payload) => {
            state.tabledata.push(payload)
        },
        UPDATE:(state,payload) => {
            let index = state.tabledata.findIndex(item => item.AccountGuid === payload.Guid);
            state.tabledata.splice(index,1,payload)
        }
    },
    actions: {
        InitCashFlow({commit},payload){
            commit('SETTING_CREATROUTER',payload.CreatRouter)
            commit('SETTING_EDITROUTER',payload.EditRouter)
            //console.log(payload)
            
            GetListByPage({pageIndex: payload.PageIndex,pageSize: payload.PageSize,keyword: payload.Keyword,advance: payload.Advance}).then(res =>{
                commit('UPDATE_TABLE_DATA',res); 
            });

            
            //commit('UPDATE_TABLE_DATA',payload.TableData);
            commit('UPDATE_TABLE_COLUMNS',payload.TableColumns);
        },
        ChangePageIndex({commit},payload){
            commit('SETTING_PAGEINDEX',payload.PageIndex);
            GetListByPage({pageIndex: payload.PageIndex,pageSize: payload.PageSize,keyword: payload.Keyword,advance: payload.Advance}).then(res =>{
                commit('UPDATE_TABLE_DATA',res); 
            });
        },
        ChangePageSize({commit},payload){
            commit('SETTING_PAGESIZE',payload.PageSize);
            GetListByPage({pageIndex: payload.PageIndex,pageSize: payload.PageSize,keyword: payload.Keyword,advance: payload.Advance}).then(res =>{
                commit('UPDATE_TABLE_DATA',res); 
            });
        },
        UpdateTableAsync({commit},payload){
            commit('SETTING_LOADTABLE',true);
            GetListByPage({pageIndex: payload.PageIndex,pageSize: payload.PageSize,keyword: payload.Keyword,advance: payload.Advance}).then(res =>{
                commit('UPDATE_TABLE_DATA',res); 
                commit('SETTING_LOADTABLE',false);
            });
        },
        CreateView({commit}){
            commit('CHANGE_TITLE',"创建流水信息")
            commit('CHANGE_MODAL');
            commit('SETTING_ID','');
            
        },
        UpdateView({commit},payload){
            commit('CHANGE_TITLE',"编辑流水信息")
            commit('CHANGE_MODAL');
            commit('SETTING_ID',payload);
        },
        Cancel({commit}){
            commit('CHANGE_MODAL');
        }
    },
    getters: { 
        tablecolumns: state => { state.tablecolumns },
        tabledata: state => state.tabledata,
        showmodal: state => state.showmodal,
        pageIndex: state => { state.pageIndex },
        pageSize: state => state.pageSize
        /* creatrouter: state => state.creatrouter,
        editrouter: state => state.editrouter */
    }
}

export default cashflow